package com.ionner.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Log4j2
@Component//一定要吧处理器添加到spring中去
public class MyMetaObjectHandler implements MetaObjectHandler {
    //插入操作
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert object");
        this.setFieldValByName("createTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        this.strictInsertFill(metaObject, "deleteFlag", Integer.class, 0);
        this.strictInsertFill(metaObject, "version", Integer.class, 1);
    }
    //更新操作
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start insert object");
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}
